package cn.shengchao.examstar.product.ohs.remote.rpc;

import cn.shengchao.examstar.product.acl.adapters.repositories.ProductCourseSettingRepository;
import cn.shengchao.examstar.product.acl.ports.repositories.IProductRepository;
import cn.shengchao.examstar.product.domain.entity.Product;
import cn.shengchao.examstar.rpc.product.api.IProductRpc;
import cn.shengchao.examstar.rpc.product.base.ContentProductTypeDefine;
import cn.shengchao.examstar.rpc.product.base.ProductPropertyDto;
import cn.shengchao.examstar.rpc.product.base.ProductSettingDto;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

/**
 * @program: examstarapi
 * @description: 课程产品管理远程调用实现
 * @author: Xiong weiqiang
 * @create: 2025-06-28 10:04
 */
@DubboService
public class CourseProductRpc implements IProductRpc {
    @Autowired
    ProductCourseSettingRepository productCourseSettingRepository;
    @Autowired
    IProductRepository productRepository;

    @Override
    public ProductPropertyDto loadProductProperty(Long productId, String productType) {
        //课程需要细分。
        Optional<Product> product=productRepository.findById(productId);
        if(product.isPresent()) {
            Product product1 = product.get();
            if (ContentProductTypeDefine.COURSE.name().equals(productType)) {
                //第三方课程一定要配，本地可以不配。查得出来按查出来的加载，查不出来默认本地课程。
                ProductSettingDto productSettingDto=productCourseSettingRepository.getProductCourseSetting(productId);
                if (productSettingDto!=null&&productSettingDto.getProductType()!=null){
                    if (productSettingDto.getProductType().equals(ContentProductTypeDefine.COURSE_GK.name())){
                        return ProductPropertyDto.buildGklCourse(productId, product1.getName().getValue(),productSettingDto.getUnitPayDay());
                    }
                }
                return ProductPropertyDto.buildLocalCourse(productId, product1.getName().getValue());
            }
            if (ContentProductTypeDefine.PRODUCT_THIRD_CAP.name().equals(productType)) {
                return ProductPropertyDto.buildCAP(productId, product1.getName().getValue());
            }
            if (ContentProductTypeDefine.PRODUCT_THIRD_YCBDC.name().equals(productType)) {
                return ProductPropertyDto.buildThirdYcbdc(productId, product1.getName().getValue());
            }
        }
        return null;
    }
}